
package scalaExec.scalaLab;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.net.URL;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;


/**   displays HTML documents  in an editor pane. */
public class EditorPaneHTMLHelp extends JFrame   { 

    public EditorPaneHTMLHelp(String htmlFile )
    {
        URL initialURL  = getClass().getResource(htmlFile);
        
      String title  = "HTML Help";
      setTitle(title);
      
      final Stack<String> urlStack = new Stack<String>();
      final JEditorPane editorPane = new JEditorPane();
      final JTextField url = new JTextField(initialURL.toString());
      
      // set up hyperlink listener
      editorPane.setEditable(false);
      
      try
      {  
                     // remember URL for back button
        urlStack.push(initialURL.toString());
                     // show URL in text field
        url.setText(initialURL.toString());
        editorPane.setPage(initialURL);
        }
        catch (IOException e)
        {  
       editorPane.setText("Exception: " + e);
       }
    
      
      editorPane.addHyperlinkListener(new  HyperlinkListener()
         {  
            public void hyperlinkUpdate(HyperlinkEvent event)
            {  
               if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
               {  
                  try
                  {  
                     // remember URL for back button
                     urlStack.push(event.getURL().toString());
                     // show URL in text field
                     url.setText(event.getURL().toString());
                     editorPane.setPage(event.getURL());
                  }
                  catch (IOException e)
                  {  
                     editorPane.setText("Exception: " + e);
                  }
               }
            }
         });

      // set up checkbox for toggling edit mode
      final JCheckBox editable = new JCheckBox();
      editable.addActionListener(new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               editorPane.setEditable(editable.isSelected());
            }
         });

      // set up load button for loading URL
    ActionListener listener = new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               try
               {  
                  // remember URL for back button
                  urlStack.push(url.getText());
                  editorPane.setPage(url.getText());
               }
               catch (IOException e)
               {  
                  editorPane.setText("Exception: " + e);
               }
            }
         };
   
      JButton loadButton = new JButton("Load");
      loadButton.addActionListener(listener);
      url.addActionListener(listener);

      // set up back button and button action

      JButton backButton = new JButton("Back");
      backButton.addActionListener(new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               if  (urlStack.size() <= 1)  return;
               try
               {  
                  // get URL from back button
                  urlStack.pop();
                  // show URL in text field
                  String urlString = urlStack.peek();
                  url.setText(urlString);
                  editorPane.setPage(urlString);
               }
               catch (IOException e)
               {  
                  editorPane.setText("Exception: " + e);
               }
            }
         });

      JPanel allPanel = new JPanel(new BorderLayout());
      
      // put all control components in a panel

      JPanel ctrlPanel = new JPanel(new BorderLayout());
      JPanel urlPanel = new JPanel();
      urlPanel.add(new JLabel("URL"));
      urlPanel.add(url);
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(loadButton);
      buttonPanel.add(backButton);
      buttonPanel.add(new JLabel("Editable"));
      buttonPanel.add(editable);
      ctrlPanel.add(buttonPanel, BorderLayout.NORTH);
      ctrlPanel.add(urlPanel, BorderLayout.CENTER);
      
      allPanel.add(ctrlPanel, BorderLayout.NORTH);
      allPanel.add(new JScrollPane(editorPane), BorderLayout.CENTER);

      add(allPanel);
     }  
        
    
    /**
   This frame contains an editor pane, a text field and button
   to enter a URL and load a document, and a Back button to 
   return to a previously loaded document.
*/
   public  EditorPaneHTMLHelp(String initialURL, String title)
   {  
      setTitle(title);
      
      final Stack<String> urlStack = new Stack<String>();
      final JEditorPane editorPane = new JEditorPane();
      final JTextField url = new JTextField(initialURL);
      
      // set up hyperlink listener
      editorPane.setEditable(false);
      
      try
      {  
                     // remember URL for back button
        urlStack.push(initialURL);
                     // show URL in text field
        url.setText(initialURL);
        editorPane.setPage(initialURL);
        }
        catch (IOException e)
        {  
       editorPane.setText("Exception: " + e);
       }
    
      
      editorPane.addHyperlinkListener(new  HyperlinkListener()
         {  
            public void hyperlinkUpdate(HyperlinkEvent event)
            {  
               if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
               {  
                  try
                  {  
                     // remember URL for back button
                     urlStack.push(event.getURL().toString());
                     // show URL in text field
                     url.setText(event.getURL().toString());
                     editorPane.setPage(event.getURL());
                  }
                  catch (IOException e)
                  {  
                     editorPane.setText("Exception: " + e);
                  }
               }
            }
         });

      // set up checkbox for toggling edit mode
      final JCheckBox editable = new JCheckBox();
      editable.addActionListener(new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               editorPane.setEditable(editable.isSelected());
            }
         });

      // set up load button for loading URL
    ActionListener listener = new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               try
               {  
                  // remember URL for back button
                  urlStack.push(url.getText());
                  editorPane.setPage(url.getText());
               }
               catch (IOException e)
               {  
                  editorPane.setText("Exception: " + e);
               }
            }
         };
   
      JButton loadButton = new JButton("Load");
      loadButton.addActionListener(listener);
      url.addActionListener(listener);

      // set up back button and button action

      JButton backButton = new JButton("Back");
      backButton.addActionListener(new  ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               if  (urlStack.size() <= 1)  return;
               try
               {  
                  // get URL from back button
                  urlStack.pop();
                  // show URL in text field
                  String urlString = urlStack.peek();
                  url.setText(urlString);
                  editorPane.setPage(urlString);
               }
               catch (IOException e)
               {  
                  editorPane.setText("Exception: " + e);
               }
            }
         });

      JPanel allPanel = new JPanel(new BorderLayout());
      
      // put all control components in a panel

      JPanel ctrlPanel = new JPanel(new BorderLayout());
      JPanel urlPanel = new JPanel();
      urlPanel.add(new JLabel("URL"));
      urlPanel.add(url);
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(loadButton);
      buttonPanel.add(backButton);
      buttonPanel.add(new JLabel("Editable"));
      buttonPanel.add(editable);
      ctrlPanel.add(buttonPanel, BorderLayout.NORTH);
      ctrlPanel.add(urlPanel, BorderLayout.CENTER);
      
      allPanel.add(ctrlPanel, BorderLayout.NORTH);
      editorPane.setSize(500, 500);
      allPanel.add(new JScrollPane(editorPane), BorderLayout.CENTER);

      add(allPanel);
      
   }

   
}
